//
//  YSGDepartmentListController.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/11/17.
//  Copyright © 2020 QDSG. All rights reserved.
//

import UIKit
/// 科室列表
class YSGDepartmentListController: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setupRefresh()
    }
    
    private var viewModel: YSGDepartmentViewModel? {
        didSet {
            guard let vm = viewModel else { return }
            
            collectionView.collectionViewLayout = vm.flowLayout()
        }
    }

    @IBOutlet private weak var collectionView: UICollectionView! {
        didSet {
            collectionView.backgroundColor = .clear
            collectionView.contentInset = UIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)
            collectionView.register(YSGHomeDepartmentItemCell.nibForCell(),
                                    forCellWithReuseIdentifier: YSGHomeDepartmentItemCell.reuseIdentifier())
        }
    }

}

// MARK: - 设置刷新
private extension YSGDepartmentListController {
    func setupRefresh() {
        collectionView.mj_header = refreshHeader(action: #selector(loadData))
        collectionView.mj_header?.beginRefreshing()
    }
    
    func endRefresh() {
        collectionView.mj_header?.endRefreshing()
    }
    
    @objc func loadData() {
        handleData()
    }
    
    func handleData() {
        YSGDepartmentViewModel.getDepartmentsList(flag: 1) { (models, error, code) in
            self.endRefresh()
            switch code {
            case .failure:
                HUDTool.showError(message: error)
            case .success:
                self.viewModel = YSGDepartmentViewModel(models: models)
            }
        }
    }
}

extension YSGDepartmentListController: UICollectionViewDataSource, UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return viewModel?.numberOfRow ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: YSGHomeDepartmentItemCell.reuseIdentifier(), for: indexPath) as? YSGHomeDepartmentItemCell else {
            return UICollectionViewCell()
        }
        
        if let vm = viewModel?.viewModel(at: indexPath) {
            cell.configure(with: vm, isHome: false)
        }
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        guard let vm = viewModel?.viewModel(at: indexPath) else { return }
        
        moreDoctorList(.all, departmentId: vm.model.id ?? "", departmentName: vm.model.name)
    }
    
    func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
        guard let cell = collectionView.cellForItem(at: indexPath) as? YSGHomeDepartmentItemCell else { return }
        cell.setSelected(true)
    }
    
    func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) {
        guard let cell = collectionView.cellForItem(at: indexPath) as? YSGHomeDepartmentItemCell else { return }
        cell.setSelected(false)
    }
    
    private func moreDoctorList(_ type: DoctorType, departmentId: String = "", departmentName: String? = nil) {
        let doctorList = DoctorReserveController(with: type, departmentId: departmentId, departmentName: departmentName)
        navigationController?.pushViewController(doctorList, animated: true)
    }
}
